package com.STS.ELearning.Classes
{
	import flash.desktop.NativeApplication;
	import flash.desktop.NativeProcess;
	import flash.desktop.NativeProcessStartupInfo;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.filesystem.File;
	import flash.filesystem.FileMode;
	import flash.filesystem.FileStream;
	import flash.net.FileReference;
	import flash.profiler.showRedrawRegions;
	
	import mx.controls.Alert;
	import mx.events.FlexEvent;
	import mx.utils.object_proxy;

	public class RunAsAdmin
	{
		private var airAdminFile:File = File.applicationDirectory.resolvePath("Main.exe");
		private var regeditFile:File = new File("C:\\Windows\\regedit.exe");
		private var regFile:File = File.applicationDirectory.resolvePath("AirAdmin.reg");
		private var cmdFile:File =  File.applicationDirectory.resolvePath("AirAdmin.bat");
		private var cmdAppFile:File = new File("C:\\Windows\\System32\\cmd.exe");
		
		public function RunAsAdmin()
		{
			if(!NativeProcess.isSupported)		
			{
				//Alert.show("Programa ders ekleyebilmek için programı yönetici olarak çalıştırmalısınız.");
				return;
			}
			getRegFile();
			
		}
		
		public function getRegFile():void
		{
		
			var regFileStream:FileStream = new FileStream;
		
			var regFileStructur:String = 'Windows Registry Editor Version 5.00';
			regFileStructur += File.lineEnding;
			regFileStructur += '[HKEY_CURRENT_USER\\Software\\Microsoft\\Windows NT\\CurrentVersion\\AppCompatFlags\\Layers]' + File.lineEnding;
			regFileStructur += '"' + airAdminFile.nativePath.replace("\\","\\\\") + '"="RUNASADMIN"' + File.lineEnding;
			
			var cmdFileStream:FileStream = new FileStream;
			var cmdFileContent:String = regeditFile.nativePath + ' "' +regFile.nativePath + '"' + File.lineEnding;
			
			
			regFile=new File(regFile.nativePath);
			regFileStream.open(regFile, FileMode.WRITE);
			regFileStream.writeUTFBytes(regFileStructur);
			regFileStream.close();
			
			cmdFile=new File(cmdFile.nativePath);
			cmdFileStream.open(cmdFile, FileMode.WRITE);
			cmdFileStream.writeUTFBytes(cmdFileContent);
			cmdFileStream.close();
			openShell();
						
			
		}
		
		// NativeProcess
		private var StartProcess:NativeProcess = new NativeProcess();
		private var args:Vector.<String> = new Vector.<String>;
		
		protected function openShell():void
		{
			
				try{
				cmdFile.openWithDefaultApplication();
				}catch(Err:Error){}
			
				var StartUpInfo:NativeProcessStartupInfo = new NativeProcessStartupInfo();
				StartUpInfo.executable = cmdAppFile;
				args.push("/C",cmdAppFile.nativePath,cmdFile.nativePath);
				StartUpInfo.arguments = args;
				if(StartProcess.running)
				{
				
					try{
						StartProcess.start(StartUpInfo);
						Alert.show("Eklendi");
					}catch(err:Error){
					Alert.show("Eklenemedi"+err);
					}
					// Your mode is as administrative right
					
				}
				else
				{
					
					try{
						StartProcess.start(StartUpInfo);
						Alert.show("Eklendi");
					}catch(err:Error){
						Alert.show("Eklenemedi"+err);
					}
					// Your mode is as administrative right
					
				}
		
		
		}
	}
}